package com.ugoodtech.mdcc.core;

import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.rtf.RtfWriter2;

import java.awt.*;
import java.io.*;

/**
 * 创建word文档 步骤:
 * 1,建立文档
 * 2,创建一个书写器
 * 3,打开文档
 * 4,向文档中写入数据
 * 5,关闭文档
 */
public class WordDemo {

    public WordDemo() {
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // 创建word文档,并设置纸张的大小
        Document document = new Document(PageSize.A4);
        try {
            RtfWriter2.getInstance(document,
                    new FileOutputStream("E:/食育项目数据模型文档.doc"));

            document.open();

            //设置合同头

            Paragraph ph = new Paragraph();
            Font f  = new Font();

            Paragraph p = new Paragraph("食育项目数据模型文档",
                    new Font(Font.NORMAL, 18, Font.BOLDITALIC, new Color(0, 0, 0)) );
            p.setAlignment(1);
            document.add(p);
            ph.setFont(f);

            // 设置中文字体
            // BaseFont bfFont =
            // BaseFont.createFont("STSongStd-Light",
//            "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            // Font chinaFont = new Font();
//    /*
//     * 创建有三列的表格
//     */
//            Table table = new Table(3);
//            document.add(new Paragraph("表格"));
//            table.setBorderWidth(1);
//            table.setBorderColor(Color.BLACK);
//            table.setPadding(0);
//            table.setSpacing(0);
//
//    /*
//     * 添加表头的元素
//     */
//            Cell cell = new Cell("");//单元格
//            table.addCell("1,1");
//            table.addCell("用java生成的表格1");
//            table.addCell("用java生成的表格1");
//
//            table.addCell("1,2");
//            table.addCell(new Paragraph("用java生成的表格2"));
//            table.addCell("用java生成的表格1");
//
//            document.add(new Paragraph("用java生成word文件"));
//            document.add(table);





            String[] aa = {
                    "Badge",
                    "Banner",

                    "ClassEntity",

                    "ClientUser",
                    "DailyRecommend",
                    "Feedback",


                    "Habit",
                    "HabitCategory",
                    "HabitCircle",
                    "HabitCircleComment",
                    "HabitSign",
                    "HabitSignRanking",


                    "News",
                    "NewsComment",
                    "NotificationMessage",

                    "PushDeviceToken",
                    "Role",
                    "School",
                    "Student",
                    "SystemUser",
                    "UserLikeLog"};
            for (String clazz:aa) {
                File file = new File("E:\\bitbucket\\diet_eduction_backend\\core\\src\\main\\java\\com\\ugoodtech\\mdcc\\core\\domain\\"+clazz+".java");
                BufferedReader reader = new BufferedReader(new FileReader(file));


                    /*
     * 创建有三列的表格
     */
                Table table = new Table(3);
                document.add(new Paragraph("数据模型"));
                table.setBorderWidth(1);
                table.setBorderColor(Color.BLACK);
                table.setPadding(0);
                table.setSpacing(0);

    /*
     * 添加表头的元素
     */
                Cell cell = new Cell("");//单元格
                table.addCell("类型");
                table.addCell("字段名");
                table.addCell("描述");





                String line = reader.readLine();
                while (line != null) {
                    int indexTable = line.indexOf("@Table");
                    if (-1 != indexTable) {
                        String[] ret1 = line.split("@Table");
                        String[] ret12 = ret1[1].split("//");
                        ret12[0] = ret12[0].replace(";", "");
                        if (ret12.length < 2) {
                            document.add(new Paragraph("表名:"+ret12[0] ));
                        } else {
                            document.add(new Paragraph("表名:"+ret12[0] + ":"+ret12[1]));
                        }
                    }
                    int index = line.indexOf("private");
                    if (-1 != index) {
                        String[] ret = line.split("private");
                        String[] ret2 = ret[1].split("//");
                        ret2[0] = ret2[0].replace(";", "");
                        try {
                            if (ret2.length < 2) {
                                String[] pp=ret2[0].split(" ");
                                try {
                                    table.addCell(pp[1]);
                                    table.addCell(pp[2]);
                                } catch (BadElementException e) {
                                    table.addCell(ret2[0]);
                                    table.addCell(ret2[0]);
                                }
                                table.addCell("");
//                                System.out.println(ret2[0]);
                            } else {
                                String[] pp=ret2[0].split(" ");
                                try {
                                    table.addCell(pp[1]);
                                    table.addCell(pp[2]);
                                } catch (BadElementException e) {
                                    table.addCell(ret2[0]);
                                    table.addCell(ret2[0]);
                                }
                                table.addCell(ret2[1]);
//                                System.out.println(ret2[0] + ":" + ret2[1]);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    //            System.out.println(line);
                    line = reader.readLine();


                }
                document.add(table);
            }

            document.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}